/***********************************************************/
/**
 * @file epd.c
 * @Synopsis  epd显示屏操作函数 
 * @author SWFighter
 * @version 1.0.0V
 * @date 2018-07-13
 */
#include "epd_io_spi.h"
#include "epd.h"

/* --------------------------------------------------------------------------*/
/**
 * @Synopsis epd显示屏引脚配置
 */
/* ----------------------------------------------------------------------------*/
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure = {0};

#ifdef USEING_DEBUG_PCB
	//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_11;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_Init(GPIOA,&GPIO_InitStructure);

#else
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 |GPIO_Pin_10; 	 //abcd OE ST LT0Êä³ö
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Init(GPIOD, &GPIO_InitStructure);

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_14 |GPIO_Pin_15;    //abcd OE ST LT0Êä³ö
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
	
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;    //abcd OE ST LT0Êä³ö
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOE, &GPIO_InitStructure);
#endif
}


/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  初始化epd显示屏
 */
/* ----------------------------------------------------------------------------*/
void EPD_W21_Init(void)
{
	GPIO_Configuration();// gpio init 
	
	EPD_W21_BS_0;		// 4 wire spi mode selected

	EPD_W21_RST_0;		// Module reset
	//driver_delay_xms(10000);
	rt_thread_delay(10);
	EPD_W21_RST_1;
	rt_thread_delay(10);
	//driver_delay_xms(10000);
	
//	EPD_W21_DispInit();		// pannel configure

//	EPD_W21_WirteLUT(LUTDefault);	// update wavefrom

}

/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  显示屏忙等待
 */
/* ----------------------------------------------------------------------------*/
void lcd_chkstatus(void)
{
	//while(1)
	{	 
   //if(isEPD_W21_BUSY==1) break;
	 rt_thread_delay(100);
	}   
	rt_kprintf("lcd none busy\n");
	
}


/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  向ped显示屏的显存写数据
 *
 * @Param data[] 数据地址
 */
/* ----------------------------------------------------------------------------*/
void epd_data_write(rt_uint8_t data[])
{
	rt_uint16_t i;

	for(i=0;i<2756;i++)
	{
		EPD_W21_WriteDATA(data[i]);
	}
}


/* --------------------------------------------------------------------------*/
/**
 * @Synopsis  epd显示屏清屏
 */
/* ----------------------------------------------------------------------------*/
void epd_data_clear(void)
{
	rt_uint16_t i;

	for(i=0;i<2756;i++)
	{
		EPD_W21_WriteDATA(0xff);
	}
}


/* --------------------------------------------------------------------------*/
/**
 * @Synopsis epd显示屏更新数据 
 *
 * @Param mode  1 更新两个屏  2 更新黑白屏 3 更新黄屏
 * @Param clear 0 不清除另外一个屏幕， 1清除另一个屏幕
 * @Param data1 显存1的现实内容
 * @Param data2 显存2的现实内容
 */
/* ----------------------------------------------------------------------------*/
void epd_show_update(rt_uint8_t mode,rt_uint8_t clear,rt_uint8_t *data1,rt_uint8_t *data2)
{
	rt_pin_write(GSM_POWR_PIN, 1);//开电源
	EPD_W21_Init();

	lcd_chkstatus();

	EPD_W21_WriteCMD(0x06);         //boost 设定
	EPD_W21_WriteDATA (0x17);
	EPD_W21_WriteDATA (0x17);
	EPD_W21_WriteDATA (0x17);

	EPD_W21_WriteCMD(0x04);         //上电
	lcd_chkstatus();                //查看芯片状态

	EPD_W21_WriteCMD(0X00);
	EPD_W21_WriteDATA(0x0f);		//选择分辨率
	EPD_W21_WriteDATA(0x0d);

	EPD_W21_WriteCMD(0X50);
	EPD_W21_WriteDATA(0x77);

	EPD_W21_WriteCMD(0x61);			//像素设定
	EPD_W21_WriteDATA (0x68);   //104 
	EPD_W21_WriteDATA (0x00);		 
	EPD_W21_WriteDATA (0xd4);		//212像素
	
	switch(mode)//模式
	{
		case 1://更新黑白黄屏
		{
			EPD_W21_WriteCMD(0x10);  //开始传输黑白图像
			epd_data_write(data1);
			EPD_W21_WriteCMD(0x13);	//开始传输黄图像
			epd_data_write(data2);
			break;
		}
		case 2://更新黑白屏
		{
			EPD_W21_WriteCMD(0x10);  //开始传输黑白图像
			epd_data_write(data1);
			if(clear)
			{
				EPD_W21_WriteCMD(0x13);	//开始传输黄图像
				epd_data_clear();
			}
			break;
		}

		case 3://更新黄屏
		{
			EPD_W21_WriteCMD(0x13);	//开始传输黄图像
			epd_data_write(data2);
			if(clear)
			{
				EPD_W21_WriteCMD(0x10);  //开始传输黑白图像
				epd_data_clear();
			}
			break;
		}
		default:
		{
			break;
		}
	}
	rt_kprintf("0x12 start\n");
	EPD_W21_WriteCMD(0x12); 
	rt_kprintf("0x12 end\n");
	lcd_chkstatus(); 

	EPD_W21_WriteCMD(0x02);			//power off
	lcd_chkstatus();

	EPD_W21_WriteCMD(0X07);			//deep sleep
	EPD_W21_WriteDATA(0xA5);
	rt_pin_write(GSM_POWR_PIN, 0);//关电源
	rt_thread_delay(400);
}

/***********************************************************
						end file
***********************************************************/

